﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using C4F.DevKit.Bluetooth.ServiceAndDeviceDiscovery;
using System.Threading;

namespace BlueBoss2
{
    public class WorkerThread
    {
        ManualResetEvent EventStop;
        ManualResetEvent EventStopped;
        WatchItem _watchItem;
        Form1 _parentForm;

        public WorkerThread(ManualResetEvent eventStop, ManualResetEvent eventStopped, Form1 form, WatchItem watchItem)
        {
            EventStop = eventStop;
            EventStopped = eventStopped;
            _parentForm = form;
            _watchItem = watchItem;
        }

        public WorkerThread(ManualResetEvent eventStop, ManualResetEvent eventStopped, Form1 form)
        {
            EventStop = eventStop;
            EventStopped = eventStopped;
            _parentForm = form;
        }

        private bool DevicePresent()
        {
            BluetoothDeviceServicesManager workerBTMgr = new BluetoothDeviceServicesManager();
            Device workerDevice = workerBTMgr.DiscoverDeviceByName(_watchItem.DeviceName);

            if (workerDevice != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public void Run(String Operation)
        {
            switch (Operation)
            {
                case "SingleDevice":
                    if (DevicePresent())
                    {
                        _parentForm.Invoke(_parentForm.AddToDeviceSeenList, new object[] {_watchItem});
                    }
                    else
                    {
                        _parentForm.Invoke(_parentForm.RemoveFromDeviceSeenList, new object[] { _watchItem });
                    }
                    break;
                case "AllDevices":
                    BluetoothDeviceServicesManager workerBTMgr = new BluetoothDeviceServicesManager();
                    List<Device> Devices = workerBTMgr.DiscoverAllDevices();
                    _parentForm.Invoke(_parentForm.ThreadUpdateDiscoverBox, new object[] { Devices });
                    break;
            }

        }
    }
}
